package projet.metadata;

import projet.CustomException;
import projet.Tools;
import projet.dataaccess.DataAccess;
import projet.dataaccess.PathErrorException;
import projet.dataaccess.XML;

public class Tag extends MetaData{
	
	//Fields
	private String name;
	private Integer dico;
	
	/**
	 * Constructeur
	 * @param name
	 * @throws CustomException 
	 */
	public Tag(int id) throws CustomException{
		super();
		this.id = id;
		this.dico = -1;
		askEdit();
	}
	
	/**
	 * Constructeur
	 * @param d XML
	 * @throws PathErrorException
	 */
	public Tag(DataAccess d) throws PathErrorException{
		super();
		this.id 	= d.getElementInt("id");
		this.name 	= d.getElementString("name");
		this.dico   = d.getElementInt("dico");
	}
	
	/**
	 * askEdit()
	 * Fait entrer au clavier les informations
	 * @throws CustomException
	 */
	public void askEdit() throws CustomException{
		String buffer;
	    System.out.print("Valeur du tag : ");
		buffer = Tools.readLineEdit(this.name);
		if (buffer.length() < 1)
			throw new CustomException("Your comment must have at least have one character !");
		this.name = buffer;
	}
	
	/**
	 * toMediaAccess()
	 * Retourne le code XML à écrire dans le fichier
	 * @return DataAccess
	 * @throws PathErrorException
	 */
	public DataAccess toMediaAccess() throws PathErrorException {
		DataAccess dataAccess = new XML();
		dataAccess.setName(this.getClass().getSimpleName().toLowerCase());
		dataAccess.addElement("id", this.id.toString());
		dataAccess.addElement("name", this.name);
		dataAccess.addElement("dico", this.dico.toString());
	
		return dataAccess;
	}
	
	/**
	 * Mutateur de name
	 * @param name
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * Accesseur pour name
	 * @return name
	 */
	public String getName(){
		return this.name;
	}
	
	/**
	 * Mutateur pour dico
	 * @param newDico
	 */
	public void setDico(int newDico){
		this.dico = newDico;
	}
	
	/**
	 * Accesseur pour dico
	 * @return
	 */
	public Integer getDico(){
		return this.dico;
	}
	
	/**
	 * toString()
	 */
	public String toString(){
		return "ID: " + this.id + " - NAME: " + this.name;
	}
	
}
